package com.istock.mock;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.istock.base.common.api.model.ApiBaseResponse;
import com.istock.base.common.api.model.RetStatusEnum;
import com.istock.state.mock.ApplyInfoService;
import com.istock.state.mock.model.ApplyInfo;

@Controller
@RequestMapping("newMock")
public class NewMockServiceController implements NewMockService {

	@Autowired
	private ApplyInfoService applyInfoService;
	private final String NO_RECORD = "NO_RECORD";
	
	@Override
	@RequestMapping("searchPay")
	@ResponseBody
	public ApiBaseResponse<String> searchPay(Long requestId) {
		ApplyInfo applyInfo = applyInfoService.searchPayApplyInfo(requestId);
		if(applyInfo == null) {
			return new ApiBaseResponse<String>(RetStatusEnum.SUCCESS,NO_RECORD,"没有记录","");
		}else {
			String returnStatus = "P";
			if(applyInfo.getStatus() == ApplyInfoStatusEnum.W.getValue()) {
				returnStatus = "P";
			}else {
				returnStatus = ApplyInfoStatusEnum.findByValue(applyInfo.getStatus()).name();
			}
			return new ApiBaseResponse<String>(RetStatusEnum.SUCCESS,returnStatus,"查询成功",applyInfo.getId()+"");
		}
	}

	@Override
	@RequestMapping("payApply")
	@ResponseBody
	public ApiBaseResponse<String> payApply(@RequestBody Map<String, Object> paramMap) {
		if(paramMap == null || paramMap.get("payId") == null) {
			return new ApiBaseResponse<String>(RetStatusEnum.FAIL , "F","payId为空" , "F");
		}
		
		if(paramMap == null || paramMap.get("amount") == null) {
			return new ApiBaseResponse<String>(RetStatusEnum.FAIL , "F","amount为空" , "F");
		}
		
		Long payId = Long.valueOf(paramMap.get("payId")+"");
		BigDecimal amount = new BigDecimal(paramMap.get("amount")+"");
		
		ApplyInfo applyInfo = null;
		if(amount.compareTo(new BigDecimal("100"))>0) {
			applyInfo = applyInfoService.createPayApplyWithAmount(payId, amount , ApplyInfoStatusEnum.S);
		}else if(amount.compareTo(new BigDecimal("10"))>0) {
			applyInfo = applyInfoService.createPayApplyWithAmount(payId, amount , ApplyInfoStatusEnum.F);
		}else {
			applyInfo = applyInfoService.createPayApplyWithAmount(payId, amount , ApplyInfoStatusEnum.P);
		}
		
		if(applyInfo.getStatus() == ApplyInfoStatusEnum.S.getValue()) {
			return new ApiBaseResponse<String>(RetStatusEnum.SUCCESS , "S","扣款成功" , ""+applyInfo.getId());
		}else if(applyInfo.getStatus() == ApplyInfoStatusEnum.F.getValue()) {
			return new ApiBaseResponse<String>(RetStatusEnum.SUCCESS , "F","扣款失败" , ""+applyInfo.getId());
		}else {
			throw new RuntimeException("amount<10 processing");
		}
		
	}

	@Override
	@RequestMapping("searchGoods")
	@ResponseBody
	public ApiBaseResponse<String> searchGoods(Long requestId) {
		ApplyInfo applyInfo = applyInfoService.searchGoodsApplyInfo(requestId);
		if(applyInfo == null) {
			return new ApiBaseResponse<String>(RetStatusEnum.SUCCESS,NO_RECORD,"没有记录","");
		}else {
			String returnStatus = "P";
			if(applyInfo.getStatus() == ApplyInfoStatusEnum.W.getValue()) {
				returnStatus = "P";
			}else {
				returnStatus = ApplyInfoStatusEnum.findByValue(applyInfo.getStatus()).name();
			}
			return new ApiBaseResponse<String>(RetStatusEnum.SUCCESS,returnStatus,"查询成功",applyInfo.getId()+"");
		}
	}

	@Override
	@RequestMapping("goodsApply")
	@ResponseBody
	public ApiBaseResponse<String> goodsApply(Long goodsId) {
		if(goodsId == null || goodsId<0) {
			return new ApiBaseResponse<String>(RetStatusEnum.FAIL , "F","goodsId为空" , "F");
		}
		
		try {
			ApplyInfo applyInfo = applyInfoService.createGoodsApply(goodsId);
			return new ApiBaseResponse<String>(RetStatusEnum.SUCCESS , "S","申请成功" , ""+applyInfo.getId());
		}catch(Exception e) {
			return new ApiBaseResponse<String>(RetStatusEnum.SUCCESS , "F","申请异常" , "F");
		}
	}

	/* 不关注结果
	 * 主要是演示一个远程调用长时间不返回,会导致本地的连接池衰竭
	 * @see com.istock.mock.MockService#badPay(com.istock.mock.MockPayModel)
	 */
	@RequestMapping("badPay")
	@ResponseBody
	public ApiBaseResponse<Map<String, Object>> badPay(MockPayModel payModel) {
		//sleep可以换成exit(0)
		//远程调用,你都无法预判远程系统会卡在什么地方
		//客户端收到唯一信息,就是timeout,这个timeout,客户端无法做任何事情,只能再次发起查询
		try {
			Thread.sleep(payModel.getAmount().longValue() * 100);
//			Thread.sleep(payModel.getAmount().longValue() * 100000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		Map<String , Object> result = new HashMap<String , Object>();
		result.put("payId", payModel.getPayId());
		result.put("applyId", 10000);
		return new ApiBaseResponse<Map<String , Object>>(RetStatusEnum.SUCCESS , "S","处理成功" , result);
	}
}
